package org.istt.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.istt.domain.ProcessStatus;
import org.istt.service.ProcessStatusManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ProcessStatusAction extends ActionSupport {
    private final Log log = LogFactory.getLog(ProcessStatusAction.class);
    private static final long serialVersionUID = 9012754123343759656L;
    private ProcessStatusManager mgr;
    private List<ProcessStatus> listProcessStatus;
    private ProcessStatus processStatus;
	public ProcessStatus getProcessStatus() {
		return processStatus;
	}

	private String id;
	private String search;
	private String cancel;
    private String delete;

	public String search() {
        if (log.isDebugEnabled()) {
            log.debug("entering 'search' method");
        }		
		if(processStatus!=null && !"".equals(processStatus.getStatus()))  {
			
			setListProcessStatus(this.mgr.searchProcessStatus(processStatus.getStatus()));
		}
		
		return SUCCESS;
	}
	
	public String loadSearch() {
        if (log.isDebugEnabled()) {
            log.debug("entering 'load search' method");
        }		
		return SUCCESS;
	}
	
	public String delete() {
        if (log.isDebugEnabled()) {
            log.debug("entering 'delete' method");
        }		
        mgr.remove(processStatus.getId());
        
        List args = new ArrayList();
        args.add(processStatus.getStatus());
        ActionContext.getContext().getSession().put("message", getText("X\u00F3a th\u00E0nh c\u00F4ng! ", args));
                
        return "delete";
    }

	public String edit() {
        // check for an add
        if (id != null) {
        	processStatus = mgr.get(NumberUtils.toLong(id));

        } else {
        	processStatus = new ProcessStatus();
        }        
        return SUCCESS;
    }

	public String execute() {
        if (cancel != null) {
            return "cancel";
        }
        if (delete != null) {
            return delete();
        }
        if (search != null) {
        	return search();
        }
        
        return SUCCESS;
    }



	public String getCancel() {
		return cancel;
	}

	public String getDelete() {
		return delete;
	}

	public String getId() {
		return id;
	}
    public ProcessStatusManager getMgr() {
		return mgr;
	}
    public String list() { 
    	listProcessStatus = mgr.getList();
        return SUCCESS; 
    }
    
    public String save() {
        if (log.isDebugEnabled()) {
            log.debug("entering 'save' method");
        }

        mgr.save(processStatus);
        
        List args = new ArrayList();
        args.add(processStatus.getStatus());
        
        ActionContext.getContext().getSession().put("message", getText("L\u01B0u d\u1EEF li\u1EC7u th\u00E0nh c\u00F4ng! ", args));
                
        return SUCCESS;
    }
    
  //Added by NDTHO.2011.12.09
	@Override
	public void validate() {
		boolean result = ((ProcessStatusManager)mgr).isUnique(processStatus.getStatus(), processStatus.getId());
		
		if (!result) {// create new entity
			addActionError(getText("processStatus.name.existed"));
		} 
	}

    public void setProcessStatus(ProcessStatus processStatus) {
		this.processStatus = processStatus;
	}
    
    public void setProcessStatusManager(ProcessStatusManager processStatusManager) {
        this.mgr = processStatusManager;
    }
    
    
    
    public void setCancel(String cancel) {
        this.cancel = cancel;
    }
    
    public void setDelete(String delete) {
        this.delete = delete;
    }
    
    public void setId(String id) {
		this.id = id;
	}
    
    public void setMgr(ProcessStatusManager mgr) {
		this.mgr = mgr;
	}


	public void setSearch(String search) {
		this.search = search;
	}

	public String getSearch() {
		return search;
	}

	public void setListProcessStatus(List<ProcessStatus> listProcessStatus) {
		this.listProcessStatus = listProcessStatus;
	}

	public List<ProcessStatus> getListProcessStatus() {
		return listProcessStatus;
	}
    
}
